package controller

import (
	"GoAdmin/models"
	. "GoAdmin/utils"
	"bytes"
	"encoding/base64"
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"github.com/astaxie/beego"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"path"
	"time"
)

func GetVersionList(c *gin.Context) {
	list, _ := new(models.Version).GetVersion()
	for k, v := range list {
		if has, city, _ := new(models.City).GetByCityId(v.District); !has {
			list[k].City = "关闭"
		} else {
			list[k].City = city.CityName
		}
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功", "data": list})
	return
}

func ChangeVersion(c *gin.Context) {
	var (
		id     string
		status string
		city   string
	)
	id = c.PostForm("id")
	status = c.PostForm("status")
	city = c.PostForm("city")
	if id == "" || status == "" {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "查询错误", "data": nil})
		return
	}
	if status == "1" { //执行关闭
		if !new(models.Version).ChangeVersion(id) {
			c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "查询错误", "data": nil})
			return
		}
		c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "处理成功", "data": nil})
		return
	} else if status == "0" { //执行打开
		if city == "" { //如果没有传城市代码，返回错误
			c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "查询错误", "data": nil})
			return
		}
		if !new(models.Version).OpenVersion(id, city) {
			c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "查询错误", "data": nil})
			return
		}
		c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "处理成功", "data": nil})
		return
	}
}

func UploadApk(c *gin.Context) {

	var (
		upload_path string
		//data        []byte
		client *oss.Client
		bucket *oss.Bucket
		//reqtype   int64
		err error
		//buf       *bytes.Buffer
		imagebyte []byte
	)
	if upload_path = c.PostForm("path"); upload_path == "" {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": "参数不正确"})
		return
	}
	//
	file_name := c.PostForm("img_name")
	sourcestring := c.PostForm("file")
	log.Println(upload_path)
	log.Println(file_name)
	log.Println(sourcestring)
	imagebyte, err = base64.StdEncoding.DecodeString(sourcestring)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Error()})
		return
	}
	// 上传到oss
	if client, err = oss.New(beego.AppConfig.String("oss_endpoint"), beego.AppConfig.String("oss_access_id"), beego.AppConfig.String("oss_access_key")); err != nil {

		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Error()})
		return
	}
	if bucket, err = client.Bucket(beego.AppConfig.String("oss_bucket")); err != nil {

		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Error()})
		return
	}
	// 构造文件名
	path.Ext(file_name)
	id := fmt.Sprint(time.Now().UnixNano()) + RandomCode(6)
	file_name = fmt.Sprintf("%s/%s/%s%s", upload_path, time.Now().Format("20060102"), id, path.Ext(file_name))
	if err = bucket.PutObject(file_name, bytes.NewReader(imagebyte)); err != nil {

		c.JSON(http.StatusOK, gin.H{"code": 201, "msg": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "上传成功", "data": gin.H{"url": beego.AppConfig.String("oss_website") + file_name}})
	return
}
